package com.atguigu.guli.service.sms.service.impl;

import com.atguigu.guli.service.sms.service.SmsService;
import com.atguigu.guli.service.sms.bean.Mcg;
import com.atguigu.guli.utils.FormUtils;
import com.atguigu.guli.utils.RandomUtils;
import com.google.gson.Gson;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class SmsServiceImpl implements SmsService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    Mcg mcg;

    @Override
    public void sendMcg(String mobile) {
        //判断手机号是否合格
        if (!FormUtils.isMobile(mobile)) {
            throw new RuntimeException("手机号不正确");
        }
        //检验一天只能上传5次,  1分钟内不能频繁上传
        //1分钟内不能发短信的key
        String minCode = "min:code"+mobile;
       if(redisTemplate.hasKey(minCode)){
           throw new RuntimeException("不能频繁上传");
       }
        //一天只能发送5次的key
        String dayCount = "day:day" + mobile;
        Object o = redisTemplate.opsForValue().get(dayCount);
        // 判断redis中有没有值
        if(o!=null){
            Integer i = (Integer)o;
            //有值判断里面的次数有没有大于5
            if(i>=5){
                throw new RuntimeException("1天只能上传5次");
            }
       }else{
            // 如果没有值说明第一次,设置缓存
            redisTemplate.opsForValue().set(dayCount, 0,24,TimeUnit.HOURS);
        }

        //生成验证码
        String code = RandomUtils.getFourBitRandom();
        //发送验证码,获取yml里配置的内容
        String host = mcg.getHost();
        String path = mcg.getPath();
        String method = mcg.getMethod();
        String appcode = mcg.getAppcode();
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", mobile);
        querys.put("param", "**code**:" + code + ",**minute**:300");
        querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");
        querys.put("templateId", "908e94ccf08b4476ba6c876d13f084ad");
        Map<String, String> bodys = new HashMap<String, String>();
        try {

            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            String result = EntityUtils.toString(response.getEntity(), "UTF-8");
            //判断发送短信有没有成功
            Gson gson = new Gson();
            Map map = gson.fromJson(result, Map.class);
            Object c = map.get("code");
            if (!"0".equals(c.toString())) {
                throw new RuntimeException("发送失败");
            }
            //生成redis的key
            String codeName = "code:code" + mobile;
            //保存验证码到redis
            redisTemplate.opsForValue().set(codeName, code,60,TimeUnit.MINUTES);

            redisTemplate.opsForValue().set(minCode, code,1, TimeUnit.MINUTES);  //设置存活时间为1分钟,

            redisTemplate.opsForValue().increment(dayCount);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("运行失败");
        }



    }
}
